<?php
/**
 * 区域营销基金预算
 * @author qianxun
 *
 */
class AreaBudget extends MY_Controller{
    function AreaBudget(){
        parent::MY_Controller();
    }
    /**
     * 专营店列表
     */
    function storeList(){
        if ($this->user['userRole'] != 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'storeList'),'title'=>'区域营销基金预算申请列表');
    
        //载入模型
        $this->load->model('areaProcessModel');
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位配置
        $this->viewData['timeUnitList'] = $this->timeUnitModel->getTimeUnitList($year);
        $where = array();
        //专营店ID条件
        $where['storeId'] = array('in', $this->userLib->getViewStoreIds());
        //年份条件
        $where['year'] = $year;
        //单店基金列表
        $procList = $this->areaProcessModel->getNewListByTimeUnit($where);
        $this->viewData['procList'] = $procList;
        $this->load->view('AreaBudget/storeList', $this->viewData);
    }
    /**
     * 区域营销基金计划申请
     */
    function storeAdd(){
        $this->load->model('areaProcessModel');
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            //处理数据
            $data = array(
                'year' => $this->input->post('year'),
                'timeUnit' => $this->input->post('timeUnit'),
                'userJob' => $this->input->post('userJob'),
                'userName' => $this->input->post('userName'),
                'userContact' => $this->input->post('userContact'),
            );
            $data['userJob'] || showError('请输入填写人职位');
            $data['userName'] || showError('请输入填写人姓名');
            //$data['userContact'] || showError('请输入联系方式');
            isMobile($data['userContact']) || showError('请输入正确的联系方式');
            if ($procId){//编辑
                $procId = $this->areaProcessModel->edit($procId, $data);
                if ($procId){
                    showSuccess('保存专营店基本信息成功', printUrl($_GET['c'], 'storeEdit', array('procId'=>$procId)));
                }else{
                    showError('保存数据失败');
                }
            }else{
                $data['storeId'] = $this->user['userId'];
                $data['createTime'] = time();
                $procId = $this->areaProcessModel->add($data);
                if ($procId){
                    showSuccess('保存专营店基本信息成功', printUrl($_GET['c'], 'storeEdit', array('procId'=>$procId)));
                }else{
                    showError('保存数据失败');
                }
            }
        }else{
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'storeList'),'title'=>'区域营销基金预算申报');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'专营店基本信息');
            //专营店模型
            $this->load->model('sellpointModel');
            $procId = (int)$this->input->get('procId');
            $procData = array();
            if ($procId){
                $procData = $this->areaProcessModel->getNewData($procId);
                $procData || showError('申报数据不存在');
                if (!$this->auth->isAreaBudgetEdit($procData)){
                    showError('您无权限申报,请确认已开通申报权限');
                }
                $storeData = $this->sellpointModel->getStoreIdData($procData['storeId']);
            }else{
                //年
                $procData['year']   = $this->input->get('year');
                $procData['year'] || showError('请选择年份');
                //月
                $procData['timeUnit']   = $this->input->get('timeUnit');
                $procData['timeUnit'] || showError('请选择时段');
                //时间单位配置
                $this->load->model('timeUnitModel');
                $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
                $procData['timeUnitStr'] = isset($timeUnitList[$procData['year']][$procData['timeUnit']]) ? $timeUnitList[$procData['year']][$procData['timeUnit']] : '';
                //专营店ID
                $data['storeId']    = $this->user['userId'];
                if (!$this->auth->isAreaBudgetAdd($procData)){
                    showError('您无权限申报,请确认已开通申报权限');
                }
                $storeData = $this->sellpointModel->getStoreIdData($this->user['userId']);
            }
            $this->viewData['procData'] = $procData;
            $this->viewData['storeData'] = $storeData;
            $this->load->view('AreaBudget/storeAdd', $this->viewData);
        }
    }
    /**
     * 专营店主线数据编辑
     */
    function storeEdit(){
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择要提交预算申请的区域营销基金');
    
            $this->load->model('areaProcessModel');
            $procData = $this->areaProcessModel->getData(array('id'=>$procId, 'storeId'=>$this->user['userId']));
            $procData || showError('区域营销基金数据不存在');
            if ($this->areaProcessModel->edit($procId, array('budgetAuditRs'=>0,'budgetApplyTime'=>time()))){
                $this->load->model('areaOnlineModel');
                $this->load->model('areaOfflineModel');
                $this->areaOnlineModel->edit(array('procId'=>$procId,'storeId'=>$this->user['userId'],'budgetAuditRs'=>-1), array('budgetAuditRs'=>0), '*');
                $this->areaOfflineModel->edit(array('procId'=>$procId,'storeId'=>$this->user['userId'],'budgetAuditRs'=>-1), array('budgetAuditRs'=>0), '*');
                //更新金额
                $this->areaProcessModel->updateMoney($procId);
                showSuccess('提交预算申请成功');
            }else{
                showError('提交预算申请失败');
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要修改的双月数据');
            $this->viewData['procId'] = $procId;
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'storeList'),'title'=>'区域营销基金预算申报');
            $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'区域营销基金预算申报详细列表');
            //主线流程数据
            $this->load->model('areaProcessModel');
            $procData = $this->areaProcessModel->getNewData(array('id'=>$procId,'storeId'=>$this->user['userId']));
            $this->viewData['procData'] = $procData;
            if (!$this->auth->isAreaBudgetEdit($procData) ){
                showError('您无权限修改');
            }
            //专营店数据
            $this->load->model('sellpointModel');
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);
            //广宣传播列表
            $this->load->model('areaOnlineModel');
            $this->viewData['onlineList'] = $this->areaOnlineModel->getNewList(array('procId'=>$procId), NULL, NULL, '', 'id desc');
            //营销活动列表
            $this->load->model('areaOfflineModel');
            $this->viewData['offlineList'] = $this->areaOfflineModel->getNewList(array('procId'=>$procId), NULL, NULL, '', 'id desc');
            //审核日志
            $this->load->model('auditLogModel');
            $this->viewData['auditList'] = $this->auditLogModel->getLogs(21, $procId);
            //操作
            $this->viewData['action'] = 'edit';
            $this->viewData['isBubao'] = 0;
            $this->load->view('AreaBudget/storeEdit', $this->viewData);
        }
    }
    /**
     * 管理端列表
     */
    function adminList(){
        if ($this->user['userRole'] == 1){
            showError('您无权限进去此页面');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'adminList'),'title'=>'区域营销基金预算管理列表');
        //载入模型
        $this->load->model('areaProcessModel');
        $this->load->model('sellpointModel');
        //查询条件
        $where = array();
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $spsarea = $this->input->get('spsarea');
        $this->viewData['spsarea'] = $spsarea;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, $spsarea, $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //年份
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        //时间单位
        $this->viewData['timeUnit'] = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] && $where['timeUnit'] = $this->viewData['timeUnit'];
        //年份条件
        $where['year'] = $year;
        //$where['budgetApply > '] =0;
        $count = $this->areaProcessModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl('', '', $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
    
        $this->viewData['procList'] = $this->areaProcessModel->getNewList($where, $limit, $page, '', 'id desc');
        $this->load->view('AreaBudget/adminList', $this->viewData);
    }
    /**
     * 预算审核
     */
    function adminAudit(){
        $this->load->model('areaProcessModel');
        $this->load->model('areaOnlineModel');
        $this->load->model('areaOfflineModel');
        $this->load->model('auditLogModel');
        if ($_POST){
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择要审核的双月数据');
            $onlineData = $this->input->post('online');//项目
            $offlineData = $this->input->post('offline');//地方车展
            if (!$onlineData && !$offlineData){
                showError('您未选择审核项');
            }
            $targetId = 0;
            if ($onlineData){
                foreach ($onlineData as $k => $v){
                    $this->areaOnlineModel->budgetAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                    $this->areaOnlineModel->updateMoney((int)$k);
                    $targetId = (int)$k;
                }
            }
            if ($offlineData){
                foreach ($offlineData as $k => $v){
                    $this->areaOfflineModel->budgetAudit((int)$k, (int)$v['budgetAuditRs'], $this->user);
                    $this->areaOfflineModel->updateMoney((int)$k);
                    $targetId = (int)$k;
                }
            }
            //更新主线流程状态和金额
            $this->areaProcessModel->budgetUpdateStatus($procId);
            $this->areaProcessModel->updateMoney($procId);
            //插入审核日志
            $this->auditLogModel->addLog(21, array(
                'procId' => $procId,
                'targetId'=>$targetId,
                'auditRole'=>$this->user['userRole'],
                'auditUid' => $this->user['userId'],
                'auditUserName' => $this->user['userName'],
                'auditRs' => 1,
                'auditDes' => '预算审核',
            ));
            showSuccess('审核成功');
        }else{
            $procId = (int)$this->input->get('procId');
            $procId || showError('请选择要审核的双月数据');
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'adminList'),'title'=>'区域营销基金预算管理列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'adminAudit'),'title'=>'区域营销基金预算审核');
            //主线流程数据
    
            $procData = $this->areaProcessModel->getNewData(array('id'=>$procId));
            $procData || showError('双月数据不存在');
            $this->viewData['procData'] = $procData;
            if (!$this->auth->isAreaBudgetAudit($this->viewData['procData'])){
                showError('您无权限审核');
            }
            //专营店数据
            $this->load->model('sellpointModel');
            $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);
            //广宣传播列表
            $this->viewData['onlineList'] = $this->areaOnlineModel->getNewList(array('procId'=>$procId,'budgetAuditRs >'=>-1), NULL, NULL, '', 'id desc');
            //营销活动列表
            $this->viewData['offlineList'] = $this->areaOfflineModel->getNewList(array('procId'=>$procId,'budgetAuditRs >'=>-1), NULL, NULL, '', 'id desc');
            //审核日志
            $this->viewData['auditList'] = $this->auditLogModel->getLogs(21, $procId);
            //页面动作
            $this->viewData['action'] = 'audit';
            $this->load->view('AreaBudget/adminAudit', $this->viewData);
        }
    }
    /**
     * 预算查看
     */
    function check(){
        $procId = (int)$this->input->get('procId');
        $procId || showError('请选择要查看的双月数据');
        //面包屑
        if ($this->user['userRole'] == 1){
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'storeList'),'title'=>'区域营销基金预算申请列表');
        }else{
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AreaBudget', 'adminList'),'title'=>'区域营销基金预算管理列表');
        }
        $this->viewData['breadcrumb'][] = array('url'=>'','title'=>'预算查看');
        //主线流程数据
        $this->load->model('areaProcessModel');
        $procData = $this->areaProcessModel->getNewData($procId);
        $this->viewData['procData'] = $procData;
        if (!$this->auth->isAreaBudgetCheck($this->viewData['procData'])){
            showError('您无权限查看');
        }
        //专营店数据
        $this->load->model('sellpointModel');
        $this->viewData['storeData'] = $this->sellpointModel->getStoreIdData($procData['storeId']);
        //广宣传播列表
        $this->load->model('areaOnlineModel');
        $this->viewData['onlineList'] = $this->areaOnlineModel->getNewList(array('procId'=>$procId), NULL, NULL, '', 'id desc');
        //营销活动列表
        $this->load->model('areaOfflineModel');
        $this->viewData['offlineList'] = $this->areaOfflineModel->getNewList(array('procId'=>$procId), NULL, NULL, '', 'id desc');
        //审核日志
        $this->load->model('auditLogModel');
        $this->viewData['auditList'] = $this->auditLogModel->getLogs(21, $procId);
        //查看标记
        $this->viewData['action'] = 'check';
        $this->load->view('AreaBudget/check', $this->viewData);
    }
}